﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Entity= VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using VietStar.Library.Commom.Module;

public partial class control_client_home : System.Web.UI.UserControl
{
    public int sttKhuyenMai = 1;
    public int sttBanChay = 1;
    public int sttXemNhieu = 1;
    public int sttLinhKien = 1;
    public int sttSanPhamVanPhong = 1;
    public int sttMayVanPhong = 1;
    Entity.SanPham entitySanPham = new Entity.SanPham();
    Business.SanPham businessSanPham = new Business.SanPham();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            LoadSanPhamKhuyenMai();
            LoadSanPhamBanChay();
            LoadSanPhamXemNhieu();
            LoadLinhKien();
            LoadSanPhamVanPhong();
            LoadMayVanPhong();
        }
    }
    //Load sản phẩm khuyến mãi
    protected void LoadSanPhamKhuyenMai()
    {
        try
        {
            DataTable dtSanPhamKhuyenMai = businessSanPham.TemplateSelectTop4KhuyenMai();
            dlSanPhamKhuyenMai.DataSource = dtSanPhamKhuyenMai;
            dlSanPhamKhuyenMai.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    // Load sản phẩm bán chạy
    protected void LoadSanPhamBanChay()
    {
        try
        {
            DataTable dtSanPhamBanChay = businessSanPham.TemplateSelectTop4BanChay();
            dlSanPhamBanChay.DataSource = dtSanPhamBanChay;
            dlSanPhamBanChay.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    // Load sản phẩm xem nhiều
    protected void LoadSanPhamXemNhieu()
    {
        try
        {
            DataTable dtSanPhamXemNhieu = businessSanPham.TemplateSelectTop4XemNhieu();
            dlSanPhamXemNhieu.DataSource = dtSanPhamXemNhieu;
            dlSanPhamXemNhieu.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    // Load linh kiện
    protected void LoadLinhKien()
    {
        try
        {
            DataTable dtLinhKien = businessSanPham.TemplateSelectTop4LinhKien();
            dlLinhKien.DataSource = dtLinhKien;
            dlLinhKien.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    // Load sản phẩm văn phòng
    protected void LoadSanPhamVanPhong()
    {
        try
        {
            DataTable dtSanPhamVanPhong = businessSanPham.TemplateSelectTop4SanPhamVanPhong();
            dlSanPhamVanPhong.DataSource = dtSanPhamVanPhong;
            dlSanPhamVanPhong.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    // Load máy văn phòng
    protected void LoadMayVanPhong()
    {
        try
        {
            DataTable dtMayVanPhong = businessSanPham.TemplateSelectTop4MayVanPhong();
            dlMayVanPhong.DataSource = dtMayVanPhong;
            dlMayVanPhong.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    protected void dlSanPhamKhuyenMai_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }
    protected void dlSanPhamBanChay_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }
    protected void dlSanPhamXemNhieu_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }

    protected void dlLinhKien_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }
    protected void dlSanPhamVanPhong_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }
    protected void dlMayVanPhong_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(e.CommandArgument.ToString());
        entitySanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = businessSanPham.SelectPrimarykey(entitySanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }

}